<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    
 *
 */
 
namespace osc\mobile\controller;
use think\Db;
use think\Session;
use think\Loader;

class Third extends MobileBase
{
		//用户图片目录
	private $user_image_dir;
	//图片保存路径
	private $upload_url;	
	protected function _initialize(){
		parent::_initialize();						
		define('UID',is_login());	
		//手机版
		if(!UID){		
			$this->redirect('login/login');					
		}

		if (!Session::has("username")) {

            $this->redirect('login/login');

        }	

        $this->user_image_dir=UID;

        $mobile = session("username");
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$this->redirect('user/index');
		}

		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.')');
		if(!$rang_order)
		{
			
		}


		$rang_order= Db::query('select * from osc_rang_order where total_time = (select max(total_time) from osc_rang_order where username = '.$mobile.')');
		
		//var_dump($rang_order);
		$num = count($rang_order);
		for($i=0;$i<$num;++$i)
		{ 
			$this->upload_url=DIR_IMAGE . 'images/'.$this->user_image_dir;
			//var_dump($this->upload_url);
			
			if (!is_dir($this->upload_url)) {
				
				mkdir($this->upload_url, 0777);
				chmod($this->upload_url, 0777);
				fopen($this->upload_url.'/index.html','wb');
			}


			$this->upload_url=DIR_IMAGE . 'images/'.$this->user_image_dir.'/'.$rang_order[$i]['id'];
			//var_dump($this->upload_url);
			
			if (!is_dir($this->upload_url)) {
				
				mkdir($this->upload_url, 0777);
				chmod($this->upload_url, 0777);
				fopen($this->upload_url.'/index.html','wb');
			}	
		}
	}



	function frist()
	{
		$mobile = session("username");
		$user = Db::name('member')->where('username',$mobile)->find();
		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.' and rang = 4) ');
		$rang_order1= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.' and rang = 3) ');
		$s1 = 0;
		$s2 = 0;
		$d1 = 0;
		if($rang_order1[0]['state']==2)
		{
			
			if($rang_order[0]['delivery']==2)
			{

						$s1 = 2;
						$s2 = 1;
						if($rang_order[0]['state']==2)
						{
							$s2 = 2;
						}
			}else
			{
						$s1 = 1;
						$s2 = 0;			
			}
		}else
		{
			if($rang_order1[0]['delivery']==2)
			{
				$d1 = 2;
			}
		}
		$this->assign('d1',$d1);
		$this->assign('s1',$s1);
		$this->assign('s2',$s2);
		$this->assign('rang_order',$rang_order);		
		$this->assign('top_title','第三轮理财');
	    $this->assign('rang_order',$rang_order);
		$this->assign('userinfo',Db::name('member')->where('uid',UID)->find());		
		$this->assign('SEO',['title'=>'第三轮理财-'.config('SITE_TITLE')]);
		$this->assign('l_url','/mobile/financing/fin');
		//$this->assign('exchange',$exchange);
		//$this->assign('variety',$variety);		
		//$this->assign('flag','user');
		
		return $this->fetch();

	}

	//提交
	function frist_delivery()
	{
		$mobile = session("username");
		$username = $mobile;
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$this->redirect('user/index');
		}
		$error = null;

		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.' and rang = 4) ');

		$rang_order1= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$username.'  and rang = 3)');
		//var_dump($rang_order1);

		if(!$rang_order)
		{
			$this->redirect('financing/fin');
		}
		//var_dump($rang_order1);
		if($rang_order1['0']['delivery']<2)
		{
			$this->redirect('financing/fin');
		}
		if($rang_order1[0]['total_time']>$rang_order[0]['total_time'])
		{
			$this->redirect('financing/fin');
		}
		$state = 1;
		//if($rang_order1[0]['state']==0 or $rang_order1[0]['state']==3)
		//{
			//$this->redirect('financing/fin');
		//}

		if(request()->isPost()){
			$data=input('post.');

			//对数据进行校验
			$data['token']  = input('post.__token__/s', null);
			$rt = Db::name('token')->where('token',$data['token'])->find();
			//var_dump($data);

			//查询提交的图片是否为空
			$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',2)->select();
			
			//$flag= true;
			if(!$img)
			{
				$error = "请上传提货的图片";
			}


			if($rt)
			{
				//$error = "请勿重复提交！";
			}
			else{
					if(!$error)
					{

						$dt['token']=$data['token'];

						$rt = Db::name('token')->insert($dt);

						Db::startTrans();
						try {
						if($rang_order)
						{
						
						

							if(!$error){
								if($rang_order['0']['delivery']==0)
								{
										//$rang['trade_dt']=$data['trade_dt'];
										$rang['delivery']=1;
										



										$rang['commit_dt']=time();
										$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);
										

										

								}
								//var_dump($rang_order);
								//判断当前记录在审核时，不能处理该订单,只有回退的订单才能处理
								if($rang_order['0']['delivery']==3)
								{
										$rang['delivery']=1;
										
										$rang['commit_dt']=time();
									$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);
									//return array('success'=>'验证成功');
								}

							}
						
						}


							Db::commit();
						} catch (\Exception $e) {

			            	Db::rollback();
			            	return array('error'=>$e->getMessage());

		       		}
		       	}
			}
	}

		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();

		//查询交易品种
		$variety = Db::name('exchage_variety')->where('state',1)->select();
        

		$rang_order= Db::query('select * from osc_rang_order where id = '.$rang_order[0]['id']);
		//var_dump($rang_order);
	     
	    $data['delete_url']='';
	    $data['upload_url']=url('Third/upload');
		//删除
		$data['delete_url']=url('Third/delete');

		$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',2)->select();
		//var_dump($img);
		$dt = time();
		$endflag=0;
		if($dt>$rang_order[0]['sell_time'])
		{
			$endflag=1;
		}
	    $this->assign('img',$img);
	    $this->assign('endflag',$endflag);	
	    $this->assign('exchange',$exchange);
	    $this->assign('rang_order',$rang_order);
	    $this->assign('error',$error);	
	    $this->assign('variety',$variety);
	    $this->assign('top_title','第三轮提货处理');
	    $this->assign('data',$data);
		$this->assign('userinfo',Db::name('member')->where('uid',UID)->find());
		$this->assign('l_url','/mobile/Third/frist');		
		$this->assign('SEO',['title'=>'第三轮提货处理-'.config('SITE_TITLE')]);
		
		$this->assign('flag','user');
		
		return $this->fetch();

	}

	function frist_rang()
	{
		$mobile = session("username");
		$username = $mobile;
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$this->redirect('user/index');
		}
		$error = null;

		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.'  and rang = 4)');
		//var_dump($rang_order);

		$rang_order1= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$username.'  and rang = 3)');
		if(!$rang_order)
		{
			$this->redirect('financing/fin');
		}
		if($rang_order1[0]['total_time']>$rang_order[0]['total_time'])
		{
			$this->redirect('financing/fin');
		}

		//进入
		if($rang_order[0]['delivery']<=1)
		{
			$this->redirect('Third/frist_delivery');
		}

		$rt = Db::name('rang_order')->where('id',$rang_order[0]['id'])->find();
			
			//var_dump($rt);
			//die;
		if($rt['trade_id']==null or $rt['trade_id']=="")
		{
			$r1 =  Db::name('rang_order')->where('total_time',$rt['total_time'])->where('rang',1)->where('username',$rang_order[0]['username'])->find();
			$dt1['trade_id'] = $r1['trade_id'];
			$dt1['sub_trade_id'] = $r1['sub_trade_id'];


				
			Db::name('rang_order')->where('id',$rang_order[0]['id'])->update($dt1);
		}

		if(request()->isPost()){
			Db::startTrans();
			$data=input('post.');
			//var_dump($data);
			//$rang_order= Db::name('rang_order')->where('id',$data['id'])->find();
			//对数据进行校验
			$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',0)->select();
			
			//$flag= true;
			if(!$img)
			{
				$error = "请上传本次交易的截图";
			}

			$data['token']  = input('post.__token__/s', null);
			$rt = Db::name('token')->where('token',$data['token'])->find();

			if($rt || $error)
			{
				#$error = "请勿重复提交！";
			}
			else{
				$dt['token']=$data['token'];

				$rt = Db::name('token')->insert($dt);
			//判断是否有图片在
			$seq = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',0)->find();
			if(!$seq)
			{
					$error="没有上传图片";
			}

			if($rang_order)
			{
				if($rang_order['0']['buy_state']==0)
				{
					
						//$rang['trade_no']=$data['trade_no'];
						//$rang['trade_dt']=$data['trade_dt'];
						//$rang['exchange_id']=$data['exchange_id'];
						$rang['buy_state']=1;
						$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);

						//$ret=Db::name('member')->where('username',$mobile)->setDec('user_rang_num',1);
					

				}
				//var_dump($rang_order);
				//判断当前记录在审核时，不能处理该订单,只有回退的订单才能处理
				if($rang_order['0']['buy_state']==3)
				{
					//$rang['trade_no']=$data['trade_no'];
					//$rang['trade_dt']=$data['trade_dt'];
					//$rang['exchange_id']=$data['exchange_id'];
					$rang['buy_state']=1;
					$ret=Db::name('rang_order')->where('id',$rang_order[0]['id'])->update($rang);
					//return array('success'=>'验证成功');
				}
				$ret=Db::name('rang_order')->where('id',$rang_order[0]['id'])->find();
				if($ret['buy_state']==1 && $ret['sell_state']==1)
				{
					$rang['state']=1;
					$ret=Db::name('rang_order')->where('id',$rang_order[0]['id'])->update($rang);
				}

			}

			Db::commit();
			$this->redirect('Third/frist');
		}
	}


		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();

		//查询交易品种
		$variety = Db::name('exchage_variety')->where('state',1)->select();
        


		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.'  and rang = 4)');
	     

	    $data['delete_url']='';
	    $data['upload_url']=url('Third/upload');
		//删除
		$data['delete_url']=url('Third/delete');

		$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',0)->select();
		//var_dump($img);
		$dt = time();
		$endflag=0;
		if($dt>$rang_order[0]['sell_time'])
		{
			$endflag=1;
		}

	    $this->assign('img',$img);
	    $this->assign('endflag',$endflag);	
	    $this->assign('exchange',$exchange);
	    $this->assign('rang_order',$rang_order);
	    $this->assign('error',$error);	
	    $this->assign('variety',$variety);
	    $this->assign('top_title','买入第三轮处理');
	    $this->assign('data',$data);
		$this->assign('userinfo',Db::name('member')->where('uid',UID)->find());		
		$this->assign('SEO',['title'=>'买入第三轮处理-'.config('SITE_TITLE')]);
		$this->assign('l_url','/mobile/Third/frist');		
		$this->assign('flag','user');
		//$this->assign('l_url','/mobile/financing/fin');
		
		return $this->fetch();
	}


	function frist_rang_sell()
	{

		$mobile = session("username");
		$username = $mobile;
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$this->redirect('user/index');
		}
		$error = null;

		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.'  and rang = 4)');
		//var_dump($rang_order);

		$rang_order1= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$username.'  and rang = 3)');
		if(!$rang_order)
		{
			$this->redirect('financing/fin');
		}
		if($rang_order1[0]['total_time']>$rang_order[0]['total_time'])
		{
			$this->redirect('financing/fin');
		}

		//进入
		if($rang_order[0]['delivery']<=1)
		{
			$this->redirect('Third/frist_delivery');
		}

		if(request()->isPost()){
			Db::startTrans();
			$data=input('post.');
			$data['token']  = input('post.__token__/s', null);
			//对数据进行校验
			

			$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',0)->select();
			
			//$flag= true;
			if(!$img)
			{
				$error = "请上传本次交易的截图";
			}

			$data['token']  = input('post.__token__/s', null);
			$rt = Db::name('token')->where('token',$data['token'])->find();

			if($rt || $error)
			{
				#$error = "请勿重复提交！";
			}
			else{
			$dt1['token']=$data['token'];

				$rt = Db::name('token')->insert($dt1);
			//判断是否有图片在
			$seq = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',1)->find();
			if(!$seq)
			{
					$error="没有上传图片";
			}

			if($rang_order)
			{
				if($rang_order['0']['state']==0)
				{
					
						//$rang['trade_no']=$data['trade_no'];
						//$rang['trade_dt']=$data['trade_dt'];
						//$rang['exchange_id']=$data['exchange_id'];
						$rang['sell_state']=1;
						$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);

						//$ret=Db::name('member')->where('username',$mobile)->setDec('user_rang_num',1);
					

				}
				//var_dump($rang_order);
				//判断当前记录在审核时，不能处理该订单,只有回退的订单才能处理
				if($rang_order['0']['state']==3)
				{
					//$rang['trade_no']=$data['trade_no'];
					//$rang['trade_dt']=$data['trade_dt'];
					//$rang['exchange_id']=$data['exchange_id'];
					$rang['sell_state']=1;
					$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);
					//return array('success'=>'验证成功');
				}
				$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->find();
				if($ret['buy_state']==1 && $ret['sell_state']==1)
				{
					$rang['state']=1;
					$ret=Db::name('rang_order')->where('id',$rang_order['0']['id'])->update($rang);
				}

			}
		}
			Db::commit();
			$this->redirect('Third/frist');
		}

		//查询交易所
		$exchange = Db::name('exchange_house')->where('state',1)->select();

		//查询交易品种
		$variety = Db::name('exchage_variety')->where('state',1)->select();
        


		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.'  and rang = 4)');
	     

	    $data['delete_url']='';
	    $data['upload_url']=url('Third/upload');
		//删除
		$data['delete_url']=url('Third/delete');

		$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',1)->select();
		//var_dump($img);
		$dt = time();
		$endflag=0;
		if($dt>$rang_order[0]['sell_time'])
		{
			$endflag=1;
		}

	    $this->assign('img',$img);
	    $this->assign('endflag',$endflag);	
	    $this->assign('exchange',$exchange);
	    $this->assign('rang_order',$rang_order);
	    $this->assign('error',$error);	
	    $this->assign('variety',$variety);
	    $this->assign('top_title','第三轮卖出处理');
	    $this->assign('data',$data);
		$this->assign('userinfo',Db::name('member')->where('uid',UID)->find());		
		$this->assign('SEO',['title'=>'第三轮卖出处理-'.config('SITE_TITLE')]);
		$this->assign('l_url','/mobile/Third/frist');		
		$this->assign('flag','user');
		//$this->assign('l_url','/mobile/financing/fin');
		
		return $this->fetch();
	}

	function exception_process()
	{
		$mobile = session("username");
		$username = $mobile;
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$this->redirect('user/index');
		}
		$error = null;
		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.'  and rang = 4)');
		//var_dump($rang_order);

		$rang_order1= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$username.'  and rang = 3)');
		if(!$rang_order)
		{
			$this->redirect('financing/fin');
		}
		if($rang_order1[0]['total_time']>$rang_order[0]['total_time'])
		{
			$this->redirect('financing/fin');
		}
		//如果需要重新提交的，清除原来的交易图片
		if(request()->isPost()){
			Db::startTrans();
			$data=input('post.');


			$data['token']  = input('post.__token__/s', null);
			$rt = Db::name('token')->where('token',$data['token'])->find();

			if($rt)
			{
				//$error = "请勿重复提交！";
			}
			else{
				$dt1['token']=$data['token'];

				$rt = Db::name('token')->insert($dt1);


				if($data['exception_desc'])
				{
					if($rang_order[0]['exception_state']==0)
					{
						$dt['exception_desc']=$data['exception_desc'];
						$dt['exception_state']=1;
						$dt['exception_time']=time();
						Db::name('rang_order')->where('id',$rang_order[0]['id'])->update($dt);
					}else if($rang_order[0]['exception_state']==3)
					{
						$dt['exception_desc']=$data['exception_desc'];
						$dt['exception_state']=1;
						//$dt['exception_time']=time();
						Db::name('rang_order')->where('id',$rang_order[0]['id'])->update($dt);
					}

				}
			}
			$this->redirect('Third/exception_process');

		}
		$img = Db::name('rang_order_img')->where('rang_order_id',$rang_order[0]['id'])->where('seq_id',4)->select();
		$this->assign('img',$img);
		 $data['delete_url']='Third/delete';
	    $data['upload_url']=url('Third/upload');
	    $this->assign('rang_order',$rang_order);
	    $this->assign('error',$error);	
	    $this->assign('top_title','异常流程处理');
	    $this->assign('data',$data);
		$this->assign('userinfo',Db::name('member')->where('uid',UID)->find());		
		$this->assign('SEO',['title'=>'异常流程处理-'.config('SITE_TITLE')]);
		$this->assign('l_url','/mobile/Third/frist');		
		$this->assign('flag','user');
		return $this->fetch();

	}

		

	

	public function upload1_d()
	{

	}

	//图片上传
	public function upload(){

		$mobile = session("username");
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$json['error'] = '请重新登录';
		}

		if(!$mobile)
		{
			$json['error'] = '请重新登录';
		}

		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.')');

		//var_dump($rang_order);
		
		$json = null;
		
		$data=input('param.');

		//var_dump($data);



		// Make sure we have the correct directory
		if (isset($data['directory'])) {
			$rang_order[0][id]=$data['id'];
			
			$d=explode('-', $data['directory']);
			$dir='';
			if(count($d)>1){
				$dir = $this->get_dir($d); 
			}else{
				$dir=$data['directory'];
			}
			//var_dump($data['directory']);			
			$this->upload_url=DIR_IMAGE . 'images/'.$this->user_image_dir.'/'.$data['id'];
			$directory = rtrim($this->upload_url.'/' . str_replace(array('../', '..\\', '..'), '',$dir), '/');

		} else {
			$this->upload_url=DIR_IMAGE . 'images/'.$this->user_image_dir.'/'.$data['id'];
			$directory = $this->upload_url;
		}




		$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.')');
		if(request()->isPost()){
			$data=input('post.');

			//对数据进行校验
			//var_dump($data);

			//判断是否有图片在
			$ret=Db::name('rang_order_img')->where('rang_order_id',$data['id'])->where('seq_id',$data['type'])->select();
			$co = count($ret);
			//var_dump($ret);
			if($co >=3)
			{
				 $json['error'] = '上传失败,最多只能上传3个文件！';
			}


		}




		$files=$_FILES;


		

		if (!$json) {
			if (!empty($files['file']['name']) && is_file($files['file']['tmp_name'])) {
				// Sanitize the filename
				$filename = basename(html_entity_decode($files['file']['name'], ENT_QUOTES, 'UTF-8'));

				//var_dump($files);

				// Validate the filename length
				if ((mb_strlen($filename) < 3) || (mb_strlen($filename) > 255)) {
					 $json['error'] = '文件名长度错误,必须大于3小于255';
				}

				$sOriginalFileName = $filename;
				$pathinfo= pathinfo($filename);
				$sExtension = $pathinfo['extension'];
				//var_dump($pathinfo);
				//$sExtension = str($filename, (strrpos($filename, '.') + 1));
				//$sExtension = strtolower($sExtension);
				$filename= date("YmdHis").rand(1, 20).".".$sExtension;

				
				
				
				
				
				// Allowed file extension types
				$allowed = array(
					'jpg',
					'jpeg',
					'gif',
					'png'
				);

				if (!in_array(mb_strtolower(mb_substr(strrchr($filename,'.'),1,mb_strlen(strrchr($filename, '.')))), $allowed)) {
					 $json['error'] = '文件类型错误';
				}

				// Allowed file mime types
				$allowed = array(
					'image/jpeg',
					'image/pjpeg',
					'image/png',
					'image/x-png',
					'image/gif'
				);

				if (!in_array($files['file']['type'], $allowed)) {
					 $json['error'] = '文件类型错误';
				}

				$falg = $this->isImg($files['file']['tmp_name']);
				//var_dump($falg);
				if(!$falg)
				{
					$json['error'] = '只支持jpg,png,gif格式图片';
				}

				//查询总的文件数量

                


				// Return any upload error
				if ($files['file']['error'] != UPLOAD_ERR_OK) {
					 $json['error'] = '上传失败'. $files['file']['error'];
				}
			} else {
				 $json['error'] = '上传失败,或者文件大小超过5M';
			}
		}

		if (!$json) {

			move_uploaded_file($files['file']['tmp_name'], $directory . '/' . $filename);
			$data=input('post.');
			//将文件信息保持到数据库中
			$fileinfo['rang_order_id']=$data['id'];


			$fileinfo['img_path']='/public/uploads/images'.'/'.UID . '/'.$data['id'].'/' . $filename;
			$fileinfo['file_name']=$filename;
			$fileinfo['seq_id']=$data['type'];
			

			$ret=Db::name('rang_order_img')->where('rang_order_id',$data['id'])->where('file_name',$filename)->find();
			if(!$ret)
			{
				$user=Db::name('rang_order_img')->insert($fileinfo);
			}else
			{
				$json['error'] = '请勿重复上传文件';
			}


			if($data['rang_num']==4)
			{
				$dt['exception_desc']=$data['exception_desc'];
				$ret=Db::name('rang_order')->where('id',$data['id'])->update($dt);
			}

			if(!$json['error'])
			{
				$json['success'] = '上传成功';
			}
			
		}
		return $json;

	}

	function isImg($fileName)
	{ 
		 $file  = fopen($fileName, "rb"); 
		 $bin  = fread($file, 2); 
		 
		 fclose($file); 
		 $strInfo = @unpack("C2chars", $bin); 
		 $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); 
		 $fileType = ''; 
	 
		 if($typeCode == 255216 /*jpg*/ || $typeCode == 7173 /*gif*/ || $typeCode == 13780 /*png*/) 
		 { 
		  return $typeCode; 
		 }
		 else
		 { 
		  // echo '"仅允许上传jpg/jpeg/gif/png格式的图片！'; 
		  return false; 
		 } 
	} 

	

	//删除图片和文件夹
	public function delete(){

		$mobile = session("username");
		$user = Db::name('member')->where('username',$mobile)->find();
		if($user['user_level']==0)
		{
			$json['error'] = '请重新登录';
		}

		if(!$mobile)
		{
			$json['error'] = '请重新登录';
		}

		//$rang_order= Db::query('select * from osc_rang_order where id = (select max(id) from osc_rang_order where username = '.$mobile.')');
		
		$json =[];
		
		$post=input('post.');
		
		if (isset($post['path'])) {
			$paths = $post['path'];
		} else {
			$json['error'] = '非法操作';
			
			return $json;			
		}
		$id = "";

		if (isset($post['id'])) {
			$id = $post['id'];
		} else {
			$json['error'] = '非法操作';
			
			return $json;			
		}

		//var_dump($paths);

		// Loop through each path to run validations
		foreach ($paths as $path) {

			//查询对应的记录是否存在
			$user = Db::name('rang_order_img')->where('img_path',$path)->find();
			//$sum = Db::name('rang_order_img')->->where('rang_order_id',$id)->select();
			//$con = count($$user);
			//if($con>=3)
			//{
				//$json['error'] = '上传图片超过了上限制';
			
				//return $json;	
			//}
			//var_dump($user);
			//然后删除对应的文件和数据库记录

			if($user)
			{
				Db::name('rang_order_img')->where('img_path',$path)->where('rang_order_id',$id)->delete();
			}

		}

		$json['success'] ='删除成功';


		return $json;
	}


	
}
